//
// Created by 崔世强 on 2018/1/3.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import UIKit
//
////定义消息的类型，先使用text，这个地方的text包含一些小的表情，如emoji、qq表情这样的，如果以后需要支持自定义表情，再说
//enum MessageType: Int {
//    case unknown = -1
//    case time = 0
//    case text = 1
//    case img = 2
//    case audio = 3
//    case video = 4
//}



protocol ChatMessageDelegate:class {
    func updateStatus()
}

class ChatMessage {
    var delegate:ChatMessageDelegate?
    var messageType:MessageType
    var chatType:ChatType = .p2p
    var me: Int
    var isMe : Bool {
        get {
            return me == 1
        }
        set {
            me = newValue ? 0 : 1
        }
    }
    var sessionId: Int
    var from: Int
    var fromName:String
    var avatar:String?
    var localId: Int?
    var syncKey: Int?
    var ts: TimeInterval = NSDate().timeIntervalSince1970
    var showTimeLabel:Bool = false
    var text: String?
    var timeStr: String {
        get {
            //            let formatter = DateFormatter()
            //            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            //            return formatter.string(from: Date(timeIntervalSince1970:ts))
            return TimeUtil.instance.chatTimeStr(ts)
        }
    }
    var audioPath:String?
    var audioDuration:Double?
    var read:Bool = true // 语音是否已读
    
    var imgFileName:String?
    var imgData:Data?
    var download:Bool?
    var imgSize:CGSize?
    var memSize:Int?
        var status:MessageStatus = .pending {
            didSet{
            if oldValue != self.status {
                self.delegate?.updateStatus()
            }
        }
    }
    
    var videoPath:String?
    var videoThumbnail:UIImage?
    var videoName:String?
    var videoWidth:CGFloat = 65
    var videoHeight:CGFloat = 120
    var videoSize:Int64?  //兆
    var videoDuration:Int64?  //毫秒
    
    var fileName:String?
    var ext:String?
    var fileSize:Int?
    var storeFile:String?
    
    //这两个是表情消息的参数
    var stickerName:String? //表情图片名
    var stickerPath:String? //表情保存地址
    var category:String? //表情所属表情包
    
    
    init(sessionId: Int, from: Int,fromName:String, me: Int = 1,messageType:MessageType = .text) {
        self.sessionId = sessionId
        self.from = from
        self.fromName = fromName
        self.me = me
        self.messageType = messageType
    }
    
    init(from: Int,me: Int = 1,messageType:MessageType = .text) {
        self.sessionId = 0
        self.from = from
        self.fromName = ""
        self.me = me
        self.messageType = messageType
    }
    
    func toMessage() -> Message {
        let msg = Message()
        msg.msgType = self.messageType.rawValue
        msg.me = self.isMe
        msg.sessionServerId = self.sessionId
        msg.chatType = self.chatType
        msg.content = self.text
        msg.userId = self.from
        msg.ts = Date(timeIntervalSince1970: TimeInterval(self.ts))
        msg.createTime = msg.ts
        return msg
    }
    
}
